//
//  WZPTableController.m
//  WZPItem
//
//  Created by 王王喆平 on 16/7/3.
//  Copyright © 2016年 UI. All rights reserved.
//

#import "WZPTableController.h"

#import "WZPEssenceController.h"
#import "WZPFriendTrendViewController.h"
#import "WZPNewViewController.h"
#import "WZPPublishController.h"
#import "WZPMeTableController.h"
#import "WZPNavigationController.h"

@interface WZPTableController ()
// 中间图片过大,所以在没被选中时也被渲染,自定义一个按钮解决
@property(nonatomic,weak) UIButton * betweenBtn;

@end

@implementation WZPTableController
-(UIButton *)betweenBtn{
    if (_betweenBtn == nil) {
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        // 图片自适应
        [btn sizeToFit];
        _betweenBtn = btn;
        [self.tabBar addSubview:btn];
    }
    return _betweenBtn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加子控制器
    [self addView];
    // 添加TabBar上的数据
    [self setupAllTitleButton];
    // 设定自定义按钮的位置
    self.betweenBtn.center =CGPointMake(self.tabBar.wzp_width * 0.5, self.tabBar.wzp_height * 0.5);
}

// 设置字体为非渲染
+(void)load{
    // 利用appearanceWhenContainedIn 来获得 当前UITabBarItem 的外观
    UITabBarItem * item = [UITabBarItem appearanceWhenContainedIn:self, nil];
    // 使用字典来保存字体选中时的颜色
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    dict[NSForegroundColorAttributeName] = [UIColor blackColor];
    [item setTitleTextAttributes:dict forState:UIControlStateSelected];
    
    // 设置文字大小 需在字体非选中状态下进行
    NSMutableDictionary * dictNor = [NSMutableDictionary dictionary];
    dictNor[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    [item setTitleTextAttributes:dictNor forState:UIControlStateNormal];
}
// 添加子控制器
-(void)addView{
    // 精华
    WZPEssenceController * essenVc = [[WZPEssenceController alloc]init];
    UINavigationController * nav = [[WZPNavigationController alloc]initWithRootViewController:essenVc];
    [self addChildViewController:nav];
    // 新帖
    WZPNewViewController * newVc = [[ WZPNewViewController alloc]init];
    UINavigationController * nav2 = [[WZPNavigationController alloc]initWithRootViewController:newVc];
    [self addChildViewController:nav2];
    // 发布
    WZPPublishController * publishVc = [[WZPPublishController alloc]init];
    [self addChildViewController:publishVc];
    // 关注
    WZPFriendTrendViewController * friedVc = [[WZPFriendTrendViewController alloc]init];
    UINavigationController * nav1 = [[WZPNavigationController alloc]initWithRootViewController:friedVc];
    [self addChildViewController:nav1];
    // 我的
    UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"WZPMeTableController" bundle:nil];
    WZPMeTableController * meVc = [storyboard instantiateInitialViewController];
    UINavigationController * nav3 = [[WZPNavigationController alloc]initWithRootViewController:meVc];
    [self addChildViewController:nav3];

}

#pragma mark 设置TabBar 上的数据
-(void)setupAllTitleButton{
   // 利用childViewControllers 来获取对应控制器
    UIViewController * vc = self.childViewControllers[0];
    vc.tabBarItem.title = @"精华";
    vc.tabBarItem.image =[UIImage imageNamed:@"tabBar_essence_icon"];
    vc.tabBarItem.selectedImage = [UIImage imageWithOriginal:@"tabBar_essence_click_icon"];
    
    UIViewController * vc1 = self.childViewControllers[1];
    vc1.tabBarItem.title = @"新帖";
    vc1.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
    vc1.tabBarItem.selectedImage = [UIImage imageWithOriginal:@"tabBar_new_click_icon"];
    
    UIViewController * vc2 = self.childViewControllers[2];
    // 禁止系统设定中间的控制器与用户交互
    vc2.tabBarItem.enabled = NO;
    
    UIViewController * vc3 = self.childViewControllers[3];
    vc3.tabBarItem.title = @"关注";
    vc3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
    vc3.tabBarItem.selectedImage = [UIImage imageWithOriginal:@"tabBar_friendTrends_click_icon"];
    
    UIViewController * vc4 = self.childViewControllers[4];
    vc4.tabBarItem.title = @"我的";
    vc4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
    vc4.tabBarItem.selectedImage = [UIImage imageWithOriginal:@"tabBar_me_click_icon"];
    
    

  }


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 0;
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
